Page({
  data: {
    surname: '',
    gender: '男',
    birthDate: '',
    preference: '',
    nameCount: 5,
    isGenerating: false,
    canGenerate: false,
    dateRange: {
      start: '',
      end: ''
    }
  },

  onLoad() {
    // 页面加载时执行
    this.initDateRange();
    this.checkCanGenerate();
  },

  // 姓氏输入处理
  onSurnameInput(e) {
    this.setData({
      surname: e.detail.value
    });
    this.checkCanGenerate();
  },

  // 性别选择处理
  selectGender(e) {
    const gender = e.currentTarget.dataset.gender;
    this.setData({
      gender: gender
    });
  },

  // 偏好输入处理
  onPreferenceInput(e) {
    this.setData({
      preference: e.detail.value
    });
  },

  // 名字数量选择处理
  selectNameCount(e) {
    const count = parseInt(e.currentTarget.dataset.count);
    this.setData({
      nameCount: count
    });
  },

  // 日期选择处理
  onDateChange(e) {
    this.setData({
      birthDate: e.detail.value
    });
  },

  // 检查是否可以生成名字
  checkCanGenerate() {
    const canGenerate = this.data.surname.trim().length > 0;
    this.setData({
      canGenerate: canGenerate
    });
  },

  // 生成名字
  generateNames() {
    if (!this.data.canGenerate || this.data.isGenerating) {
      return;
    }

    // 验证输入
    if (!this.data.surname.trim()) {
      wx.showToast({
        title: '请输入宝宝姓氏',
        icon: 'none'
      });
      return;
    }

    this.setData({
      isGenerating: true
    });

    // 模拟API调用延迟
    setTimeout(() => {
      // 这里应该调用实际的AI API
      // 目前使用模拟数据
      const mockNames = this.generateMockNames();
      
      // 跳转到结果页面
      wx.navigateTo({
        url: '/pages/result/result',
        success: () => {
          // 传递数据到结果页面
          getApp().globalData.generatedNames = mockNames;
          getApp().globalData.userInput = {
            surname: this.data.surname,
            gender: this.data.gender,
            birthDate: this.data.birthDate,
            preference: this.data.preference
          };
        }
      });

      this.setData({
        isGenerating: false
      });
    }, 2000);
  },

  // 生成模拟名字数据（实际项目中应该调用AI API）
  generateMockNames() {
    const { surname, gender, preference, nameCount } = this.data;
    const names = [];
    
    // 男孩名字库
    const boyNames = [
      { name: '浩然', meaning: '浩然正气，品德高尚', origin: '出自《孟子》"浩然之气"', pinyin: 'hào rán' },
      { name: '子轩', meaning: '气宇轩昂，风度翩翩', origin: '古典文学中形容男子气概', pinyin: 'zǐ xuān' },
      { name: '文博', meaning: '文采飞扬，博学多才', origin: '寓意学识渊博，文采出众', pinyin: 'wén bó' },
      { name: '志远', meaning: '志向远大，目标明确', origin: '出自"志存高远"', pinyin: 'zhì yuǎn' },
      { name: '天佑', meaning: '上天保佑，福泽深厚', origin: '传统吉祥寓意', pinyin: 'tiān yòu' },
      { name: '俊杰', meaning: '英俊杰出，才华横溢', origin: '形容优秀出众的人才', pinyin: 'jùn jié' },
      { name: '博文', meaning: '博学多文，知识丰富', origin: '出自"博文约礼"', pinyin: 'bó wén' },
      { name: '承志', meaning: '继承志向，传承精神', origin: '寓意继承家族优良传统', pinyin: 'chéng zhì' }
    ];

    // 女孩名字库
    const girlNames = [
      { name: '雅琪', meaning: '优雅美丽，珍贵美好', origin: '寓意品德高雅，如美玉般珍贵', pinyin: 'yǎ qí' },
      { name: '诗涵', meaning: '诗意涵养，文雅有才', origin: '寓意有文学修养，内涵丰富', pinyin: 'shī hán' },
      { name: '梦瑶', meaning: '美梦如瑶，美好如画', origin: '寓意梦想美好，如美玉般珍贵', pinyin: 'mèng yáo' },
      { name: '欣怡', meaning: '欣欣向荣，怡然自得', origin: '寓意快乐愉悦，心情舒畅', pinyin: 'xīn yí' },
      { name: '雨桐', meaning: '雨润梧桐，茁壮成长', origin: '寓意如雨露滋润，健康成长', pinyin: 'yǔ tóng' },
      { name: '思颖', meaning: '思维敏捷，聪颖智慧', origin: '寓意聪明伶俐，思维活跃', pinyin: 'sī yǐng' },
      { name: '婉清', meaning: '温婉清雅，气质优雅', origin: '寓意性格温和，气质清雅', pinyin: 'wǎn qīng' },
      { name: '语嫣', meaning: '语笑嫣然，美丽动人', origin: '寓意说话动听，笑容美丽', pinyin: 'yǔ yān' }
    ];

    const namePool = gender === '男' ? boyNames : girlNames;
    
    // 随机选择指定数量的名字
    for (let i = 0; i < nameCount; i++) {
      const randomIndex = Math.floor(Math.random() * namePool.length);
      const selectedName = namePool[randomIndex];
      
      names.push({
        fullName: surname + selectedName.name,
        firstName: selectedName.name,
        meaning: selectedName.meaning,
        origin: selectedName.origin,
        pinyin: selectedName.pinyin,
        isFavorite: false
      });
      
      // 避免重复
      namePool.splice(randomIndex, 1);
      if (namePool.length === 0) break;
    }

         return names;
   },

   // 初始化日期范围（前15天到后15天）
   initDateRange() {
     const today = new Date();
     const startDate = new Date(today);
     const endDate = new Date(today);
     
     // 前15天
     startDate.setDate(today.getDate() - 15);
     // 后15天
     endDate.setDate(today.getDate() + 15);
     
     // 格式化日期为 YYYY-MM-DD
     const formatDate = (date) => {
       const year = date.getFullYear();
       const month = String(date.getMonth() + 1).padStart(2, '0');
       const day = String(date.getDate()).padStart(2, '0');
       return `${year}-${month}-${day}`;
     };
     
     this.setData({
       dateRange: {
         start: formatDate(startDate),
         end: formatDate(endDate)
       }
     });
   }
 });
